package com.samsung.spen.sshare.main.image;

import android.R.bool;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;

public class RecyclingBitmapDrawable extends BitmapDrawable{
	static final String LOG_TAG = "CountingBitmapDrawable";
	
	private int mCacheRefCount = 0;
	private int mDisplayRefCount = 0;
	
	private boolean mHasBeenDisplayed;
	
	public RecyclingBitmapDrawable(Resources res, Bitmap bitmap){
		super(res,bitmap);
	}
	
	public void setIsDisplayed(boolean isDisplayed){
		synchronized (this) {
			if(isDisplayed){
				mDisplayRefCount++;
				mHasBeenDisplayed = true;
			} else {
				mDisplayRefCount--;
			}
		}
		
		checkState();
	}
	
	public void setIsCached(boolean isCached){
		synchronized (this) {
			if(isCached){
				mCacheRefCount++;
			} else {
				mCacheRefCount--;
			}
			
			checkState();
		}
	}

	private synchronized void checkState() {
		if(mCacheRefCount <= 0 && mDisplayRefCount <=0 && mHasBeenDisplayed && hasValidBitmap()){
			Log.d(LOG_TAG, "No longer being used or cached so recycling. "
                    + toString());
			getBitmap().recycle();
		}
	}
	
	public synchronized boolean hasValidBitmap(){
		Bitmap bitmap = getBitmap();
		return bitmap != null && !bitmap.isRecycled();
	}
}
